<?php
namespace app\classroom\controller;
use app\classroom\logic\LearnLogic;
use think\Db;

/**
 * 每日学习api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/27
 * Time: 09:44
 */
class Learn extends Base {

    public function __construct(){
        parent::__construct();
        $this->l_logic = new LearnLogic();
    }

    /**
     * 开始学习
     * @return \think\response\Json
     */
    public function ajax_start_learn(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->l_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查开始学习规则
        $r = $this->l_logic->check_start_leran_rule($u_detail['openid'],$this->clid);
        if($r['code'] == 0){
            return json($r);
        }elseif($r['code'] == 1){
            Db::startTrans();
            //开始新的答题
            $r = $this->l_logic->start_new_learn($u_detail['openid'],$this->clid);
            if(!$r['code']){
                Db::rollback();
                return json($r);
            }
            $clcid = $r['data']['clcid'];

            //生成试卷
            $r = $this->l_logic->gen_paper($u_detail['openid'],$clcid,$this->clid);
            if(!$r['code']){
                Db::rollback();
                return json($r);
            }
            Db::commit();
        }elseif($r['code'] == 2){
            $clcid = $r['data']['clcid'];
        }

        $data['clcid'] = $clcid;
        return json($this->l_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取学习题目
     * @return \think\response\Json
     */
    public function ajax_get_learn_ti(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->l_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $r = $this->l_logic->get_learn_ti($u_detail['openid'],$params['clcid'],$this->clid);
        if(!$r['code']){
            return json($r);
        }

        $data['list'] = $r['data']['list'];
        return json($this->l_logic->get_response(1,'OK',$data));
    }

    /**
     * 提交学习答案
     * @return \think\response\Json
     */
    public function ajax_sub_learn_anwer(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->l_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查提交学习答案规则
        $r = $this->l_logic->check_sub_learn_anwser_rule($u_detail['openid'],$params['clcid'],$this->clid);
        if(!$r['code']){
            return json($r);
        }
        $clc_detail = $r['data']['clc_detail'];

        $maps = [];
        $maps[] = ['id','=',$params['clrid']];
        $maps[] = ['openid','=',$u_detail['openid']];
        $maps[] = ['clid','=',$this->clid];
        $maps[] = ['clcid','=',$clc_detail['id']];

        $clr_data = [];
        $clr_data['answer'] = $params['answer'];
        $r = model('class_learn_record')->force()->save($clr_data,$maps);
        if($r !== false){
            return json($this->l_logic->get_response(1,'OK'));
        }
        return json($this->l_logic->get_response(0,'提交失败'));
    }

    /**
     * 预提交
     * @return \think\response\Json
     */
    public function ajax_pre_sub(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->l_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查预提交规则
        $r = $this->l_logic->check_pre_sub_rule($u_detail['openid'],$params['clcid'],$this->clid);
        if(!$r['code']){
            return json($r);
        }
        $clc_detail = $r['data']['clc_detail'];

        //预提交，计算答题对与错的数量
        $r = $this->l_logic->pre_sub($u_detail['openid'],$clc_detail['id'],$this->clid);
        if(!$r['code']){
            return json($r);
        }

        $data['right_num'] = $r['data']['right_num'];
        $data['wrong_num'] = $r['data']['wrong_num'];
        $data['sum_num'] = $r['data']['sum_num'];
        return json($this->l_logic->get_response(1,'OK',$data));
    }

    /**
     * 真提交
     * @return \think\response\Json
     */
    public function ajax_real_sub(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->l_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查真提交规则
        $r = $this->l_logic->check_real_sub_rule($u_detail['openid'],$params['clcid'],$this->clid);
        if(!$r['code']){
            return json($r);
        }
        $clc_detail = $r['data']['clc_detail'];

        //真提交
        $r = $this->l_logic->real_sub($u_detail['openid'],$clc_detail['id'],$this->clid);
        if(!$r['code']){
            return json($r);
        }

        $maps = [];
        $maps[] = ['id','=',$clc_detail['id']];
        $maps[] = ['openid','=',$u_detail['openid']];
        $maps[] = ['clid','=',$this->clid];
        $clc_detail = model('class_learn_ci')->get_detail($maps,'right_num,wrong_num,score,zeng_score');

        $data['lianxu_day'] = $r['data']['lianxu_day'];
        $data['score'] = $clc_detail['score'];
        $data['zeng_score'] = $clc_detail['zeng_score'];
        return json($this->l_logic->get_response(1,'保存成功',$data));
    }
}
